{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ab3a1459",
   "metadata": {},
   "source": [
    "## 1. 文件,open as\n",
    "### 1.1 读取方式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9d8dd977",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 默认写入的是国标，\n",
    "with open(\"../src/data/open_write.txt\", \"wt\", encoding=\"utf-8\") as out_file:\n",
    "    out_file.write(\"该文本会写入到文件中\\n看到我了吧！\")\n",
    "    # out_file.writelines(list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "00b490d5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "该文本会写入到文件中\n",
      "看到我了吧！\n"
     ]
    }
   ],
   "source": [
    "# 全部读取\n",
    "with open('../src/data/open_write.txt', 'r', encoding='utf-8') as f:\n",
    "    print(f.read())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3ac17f93",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "该文本会写入到文件中\n",
      "\n",
      "看到我了吧！\n"
     ]
    }
   ],
   "source": [
    "# 逐行读取\n",
    "with open('../src/data/open_write.txt', 'r', encoding='utf-8') as f:\n",
    "    for line in f:\n",
    "        print(line)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "0e010929",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['该文本会写入到文件中\\n', '看到我了吧！']\n"
     ]
    }
   ],
   "source": [
    "# 读取所有行到列表\n",
    "with open('../src/data/open_write.txt', 'r', encoding='utf-8') as f:\n",
    "    lines = f.readlines()\n",
    "    # f.readline() # 读取一行\n",
    "    print(lines)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3da9b4bf",
   "metadata": {},
   "source": [
    "### 1.2 w,r模式"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1e364fb5",
   "metadata": {},
   "source": [
    "#### 文本模式（默认）   \n",
    "file = open(\"data.txt\", \"r\")     # 只读文本，指针在文件头   \n",
    "file = open(\"data.txt\", \"w\")     # 只写文本，先清空文件内容    \n",
    "file = open(\"data.txt\", \"a\")     # 追加文本，不能读  \n",
    "\\+                               # 使得可读写\n",
    "\n",
    "#### 二进制模式  \n",
    "file = open(\"image.jpg\", \"rb\")   # 只读二进制  \n",
    "file = open(\"data.bin\", \"wb\")    # 只写二进制  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cc03f18d",
   "metadata": {},
   "source": [
    "### 1.3 属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ba929757",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The Tragedy Of Romeo And Juliet \n",
      "\n",
      "<_io.TextIOWrapper name='C:\\\\Users\\\\zhengyang\\\\learning\\\\Python\\\\src\\\\data\\\\罗密欧与朱丽叶(英文版)莎士比亚.txt' mode='r' encoding='utf-8'>\n"
     ]
    }
   ],
   "source": [
    "with open(r\"C:\\Users\\zhengyang\\learning\\Python\\src\\data\\罗密欧与朱丽叶(英文版)莎士比亚.txt\", 'r', encoding='utf-8') as f:\n",
    "    line=f.__next__() #这是第一行\n",
    "    processed_line = line.title()\n",
    "    print(processed_line)\n",
    "print(f)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5e4bbd71",
   "metadata": {},
   "source": [
    "### 1.4 不建议"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "3a2dccd4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 手动关闭方式\n",
    "a = open(r\"C:\\Users\\zhengyang\\learning\\Python\\src\\data\\罗密欧与朱丽叶(英文版)莎士比亚.txt\")\n",
    "a.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c013cf15",
   "metadata": {},
   "source": [
    "## 2. 字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a014081a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Abc'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'abc'.title()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "7e2ec07e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'函数_类.ipynbd'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'''  \n",
    "函数_类.ipynbd   '''.strip() # 去掉字符串首尾的空格"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "999296de",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "mining",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
